package com.study.sbsummary.utils;

import com.google.common.base.Strings;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * //                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.
 * //             __.'              ~.   .~              `.__
 * //           .'//        NO        \./        BUG        \\`.
 * //         .'//                     |                     \\`.
 * //       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
 * //     .'//.-"                 `-.  |  .-'                 "-.\\`.
 * //   .'//______.============-..   \ | /   ..-============.______\\`.
 * // .'______________________________\|/______________________________`.
 * //                       KEEP HUNGRY，KEEP FOOLISH
 * <p>
 * <p>
 * 字符串工具类
 *
 * @author hky
 */
public class StringUtil {

    private StringUtil() {
    }

    public static boolean isEmpty(Object obj) {
        return (obj == null || obj.toString().trim().length() == 0);
    }

    public static String getLocalIp() {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            return "127.0.0.1";
        }
        String localIp = address.getHostAddress();
        String lastNumber = localIp.split("\\.")[3];

        return lastNumber;
    }

    /**获取上一个季度(格式xxxx_qx)*/
    public static String getLastQuarterOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int quarter  = calendar.get(Calendar.MONTH) / 3 +1;
        //如果是1季度，需要获取上一年第4季度
        if(quarter == 1){
            calendar.add(Calendar.YEAR, -1);
            quarter = 4 ;
        }else{
            quarter = quarter -1;
        }
        return calendar.get(Calendar.YEAR)+"_q"+quarter;
    }

    /**
     * 将map转成字符串
     * @param map
     * @return
     */
    public static String convertMap2String(Map<String,String> map,String separator) {
        StringBuilder sb=new StringBuilder();
        for(Map.Entry<String,String> ent:map.entrySet()){
                sb.append(ent.getKey()).append("=").append(ent.getValue()).append(separator);
        }
        if (sb.length()>0){
            sb=sb.deleteCharAt(sb.length()-1);
        }
        return sb.toString();
    }

    public static boolean noNullOrEmpty(String... strings) {
        for (String string : strings) {
            if(string == null || string.equals("")) {
                return false;
            }
        }
        return true;
    }

    public static boolean hasNullOrEmpty(String... strings) {
        return !noNullOrEmpty(strings);
    }

    /**
     * 版本号比较
     *
     * @param v1
     * @param v2
     * @return 0代表相等，1代表左边大，-1代表右边大
     * Utils.compareVersion("1.0.358_20180820090554","1.0.358_20180820090553")=1
     */
    public static int compareVersion(String v1, String v2) {
        if (Strings.isNullOrEmpty(v1) || Strings.isNullOrEmpty(v2)){
            return 0;
        }
        if (v1.equals(v2)) {
            return 0;
        }
        String[] version1Array = v1.split("[._]");
        String[] version2Array = v2.split("[._]");
        int index = 0;
        int minLen = Math.min(version1Array.length, version2Array.length);
        long diff = 0;

        while (index < minLen
                && (diff = Long.parseLong(version1Array[index])
                - Long.parseLong(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            for (int i = index; i < version1Array.length; i++) {
                if (Long.parseLong(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Long.parseLong(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }

    public static void main(String[] args) {
        System.out.println(getLocalIp());
    }


}
